﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using System;

public enum OperatorState
{
    BeginTurn,
    Round,
    EndTurn,
    EnemyQuit,
    PlayerQuit
}


public class GameControl : MonoBehaviour {

    public static GameControl _instance;

    public FishGenerate fg;
    public BuffGenerate bg;

    public List<GameObject> battleFish = new List<GameObject>();
    public List<GameObject> heroFish = new List<GameObject>();
    public List<GameObject> enemyFish = new List<GameObject>();

    public GameObject FishGo;
    [HideInInspector]
    public List<int> myHero = new List<int>();
    [HideInInspector]
    public List<int> myStane = new List<int>();
    [HideInInspector]
    public List<int> EnemyHero = new List<int>();
    [HideInInspector]
    public List<int> EnemyStance = new List<int>();

    private int turnNum;
    //private int roundNum;
    private int attackOrder;
    public OperatorState mState;

    private void Awake()
    {
        _instance = this;
        InitFishDict();
        //fg = GetComponent<FishGenerate>();
        //fg.GenerateFish();
        //InitFish();
    }

    void Start()
    {
        //Invoke("ButtleTurn", 1);
    }

    //初始化鱼的字典
    public void InitFishDict()
    {
        fg = GetComponent<FishGenerate>();
        fg.GenerateFish();
        bg = GetComponent<BuffGenerate>();
        bg.GenerateBuff();
    }

    //初始化鱼
    public void InitFish()
    {
        //清除之前的数据
        foreach (GameObject go in battleFish)
        {
            Destroy(go);
        }
        battleFish.Clear();
        heroFish.Clear();
        enemyFish.Clear();

        for (int i = 0; i < myHero.Count; i++)
        {
            //获得这条鱼的数据
            FishData tempFdata;
            fg.DictFish.TryGetValue(myHero[i], out tempFdata);
            FishData fdata = new FishData();
            fdata.SetMySelf(tempFdata);

            //获得这条鱼的Gameboject
            GameObject parentGo = GameObject.Find("e" + myStane[i]);
            GameObject go = Instantiate(FishGo, parentGo.transform);
            go.name = "Hero" + fdata.FishID1.ToString();
            go.tag = "Hero";

            //将鱼加入阵营
            battleFish.Add(go);
            heroFish.Add(go);

            //将鱼的位置，data等信息加入fishview
            FishView fv = go.GetComponent<FishView>();
            fv.fishPos = myStane[i];
            fv.fdata = fdata;
        }

        for (int i = 0; i < EnemyHero.Count; i++)
        {
            //获得这条鱼的数据
            FishData tempFdata;
            fg.DictFish.TryGetValue(EnemyHero[i], out tempFdata);
            FishData fdata = new FishData();
            fdata.SetMySelf(tempFdata);

            //获得这条鱼的Gameboject
            GameObject parentGo = GameObject.Find("p" + EnemyStance[i]);
            GameObject go = Instantiate(FishGo, parentGo.transform);
            go.name = "Enemy" + fdata.FishID1.ToString();
            go.tag = "Enemy";

            //将鱼加入阵营
            battleFish.Add(go);
            enemyFish.Add(go);

            //将鱼的位置，data等信息加入fishview
            FishView fv = go.GetComponent<FishView>();
            fv.fishPos = EnemyStance[i];
            fv.fdata = fdata;
        }
        FishSort();
        turnNum = 0;
        //roundNum = 0;
        attackOrder = 0;
        mState = OperatorState.BeginTurn;
    }

    //对鱼进行排序
    void FishSort()
    {
        battleFish.Sort(delegate (GameObject x, GameObject y)
        {
            FishView fv1 = x.GetComponent<FishView>();
            FishView fv2 = y.GetComponent<FishView>();

            return fv2.fdata.Sp.CompareTo(fv1.fdata.Sp);
        });
    }

    //打乱List排序
    public List<GameObject> RandomSortList(List<GameObject> list)
    {
        int currentIndex;
        GameObject temp;
        for (int i = 0; i < list.Count; i++)
        {
            currentIndex = new System.Random().Next(0, list.Count - i);
            temp = list[currentIndex];
            list[currentIndex] = list[list.Count - 1 - i];
            list[list.Count - 1 - i] = temp;
        }
        return list;
    }

    //控制战斗循环
    public  void ButtleTurn()
    {
        switch (mState)
        {
            case OperatorState.BeginTurn:
                TurnBegin();
                break;
            case OperatorState.Round:
                FishAct();
                break;
            case OperatorState.EndTurn:
                mState = OperatorState.BeginTurn;
                ButtleTurn();
                break;
            case OperatorState.EnemyQuit:
                print("你赢了！");
                break;
            case OperatorState.PlayerQuit:
                print("你输了！");
                break;
        }
    }

    //轮到鱼行动
    void FishAct()
    {
        //roundNum += 1;
        //print("出手数" + roundNum);
        if (attackOrder > battleFish.Count - 1)
        {
            attackOrder = 0;
            //print("速度排位" + attackOrder);
            mState = OperatorState.EndTurn;
            ButtleTurn();
        }
        else
        {
            //print("速度排位" + attackOrder);
            FishAttack fa = battleFish[attackOrder].GetComponent<FishAttack>();
            //print("攻击者名称    " + battleFish[attackOrder].name);
            attackOrder++;

            //fa.BuffEffect();
            fa.LookTarget();
        }
    }

    //回合开始
    void TurnBegin()
    {
        turnNum += 1;
        print("回合" + turnNum+ "-----------------------------------");
        FishSort();

        foreach (GameObject go in battleFish)
        {

            go.GetComponent<EmBattle>().AddMana();
        }

        mState = OperatorState.Round;
        ButtleTurn();
    }

    //删除鱼
    public void DeleteHero(GameObject go)
    {
        for(int i = 0 ; i < battleFish.Count; i++)
        {
            if (go.name == battleFish[i].name)
            {
                battleFish.Remove(battleFish[i]);
            }
        }
        for (int i = 0; i < heroFish.Count; i++)
        {
            if (go.name == heroFish[i].name)
            {
                heroFish.Remove(heroFish[i]);
            }
        }
        for (int i = 0; i < enemyFish.Count; i++)
        {
            if (go.name == enemyFish[i].name)
            {
                enemyFish.Remove(enemyFish[i]);
            }
        }

        Destroy(go);
        print(go.name + "死亡了");
        //FishSort();
        GameOver();
    }

    //游戏结束
    void GameOver()
    {
        if (heroFish.Count <= 0)
        {
            mState = OperatorState.PlayerQuit;
            ButtleTurn();
        }
        if (enemyFish.Count <= 0)
        {
            mState = OperatorState.EnemyQuit;
            ButtleTurn();
        }
    }
}
